package com.cellulargames.util;

import com.cellulargames.model.Carta;

public class Util {

	public static void quicksort(Carta[] a) {
		quicksort(a, 0, a.length - 1);
	}

	// quicksort a[left] to a[right]
	public static void quicksort(Carta[] a, int left, int right) {
		if (right <= left) return;
		int i = partition(a, left, right);
		quicksort(a, left, i-1);
		quicksort(a, i+1, right);
	}

	// partition a[left] to a[right], assumes left < right
	private static int partition(Carta[] a, int left, int right) {
		int i = left - 1;
		int j = right;
		while (true) {
			while (a[++i].valor < a[right].valor)           // find item on left to swap
				;                               // a[right] acts as sentinel
			while (a[right].valor < a[--j].valor)           // find item on right to swap
				if (j == left) break;           // don't go out-of-bounds
			if (i >= j) break;                  // check if pointers cross
			exch(a, i, j);                      // swap two elements into place
		}
		exch(a, i, right);                      // swap with partition element
		return i;
	}

	// exchange a[i] and a[j]
	private static void exch(Carta[] a, int i, int j) {
		Carta swap = a[i];
		a[i] = a[j];
		a[j] = swap;
		//System.out.println("Swap: "+a[i]+" y "+a[j]);
	}


}	
